﻿#include "DirectionalLightComponent.h"

#include "Scene/SceneNode.h"
#include "Scene/Scene.h"

#include "Render/Scene/RenderDirectionalLightData.h"
#include "Render/RenderContext.h"
#include "Render/Scene/SceneGlobal.h"


void RDirectionalLightComponent::Paint(CRenderScene* InRenderData)
{
    Super::Paint(InRenderData);

    SRenderDirectionalLightData& LightData=InRenderData->DirectionalLights.Emplace();
    LightData.Rotation=GetNode()->GetWorldRotation();
    LightData.Color=Color;
    LightData.Intensity=Intensity;
    LightData.MaxCastShadowDistance=DistanceToTarget;
    LightData.ShadowBias=ShadowBias;

    //检查级联阴影的最大深度是否合法
    LightData.CascadeMaxDepths=CascadeMaxDepths;
    LightData.CascadeMaxDepths.Resize(CSceneGlobal::GetCascadeNum());//最多支持4级
    for(int64_t i=0;i<LightData.CascadeMaxDepths.Num();++i)
    {
        if(i==0)
        {
            if(LightData.CascadeMaxDepths[i]<=0)
            {
                LightData.CascadeMaxDepths[i]=1;
            }
        }
        else
        {
            if(LightData.CascadeMaxDepths[i]<=LightData.CascadeMaxDepths[i-1])
            {
                LightData.CascadeMaxDepths[i]=LightData.CascadeMaxDepths[i-1]+1;
            }
        }
    }
    while(LightData.CascadeMaxDepths.Num()< CSceneGlobal::GetCascadeNum())
    {
        LightData.CascadeMaxDepths.Add(LightData.CascadeMaxDepths.Last()*2);
    }

}

